package top.sqdpt.third.factory;

import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import top.sqdpt.third.strategy.WeatherStrategy;
import top.sqdpt.third.utils.LoadBalancerUtil;
import top.sqdpt.third.enums.WeatherType;

/**
 * @author pengtao
 * @date 2025/4/7
 * @Descciption
 **/
@Component
public class WeatherFactory {
    private final Map<WeatherType, WeatherStrategy> strategyMap;

    public WeatherFactory(List<WeatherStrategy> strategies) {
        //判空
        if (strategies == null || strategies.isEmpty()) {
            throw new IllegalArgumentException("strategies不能为空");
        }
        //根据WeatherType分组   
        strategyMap = strategies.stream().collect(Collectors.toMap(WeatherStrategy::getWeatherType, x -> x));
    }

    /**
     * 根据WeatherType获取策略
     *
     * @param type
     * @return
     */
    public WeatherStrategy getStrategy(WeatherType type) {
        return strategyMap.get(type);
    }

    /**
     * 随机获取策略
     *
     * @return
     */
    public WeatherStrategy getStrategyRandom() {
        //随机获取一个策略
        WeatherType type = LoadBalancerUtil.random(Arrays.asList(WeatherType.values()));
        return getStrategy(type);
    }


}
